<script src="downloader.js"></script>

<script>

	var EslRssUrl="http://feeds.feedburner.com/EnglishAsASecondLanguagePodcast"
	
	function createRequest()
	{
		var request = false;
		try {
			request = new XMLHttpRequest();
		} 
		catch (trymicrosoft) {
			try {
				request = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (othermicrosoft) {
			try {
				request = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (failed) {
				request = false;
				}
			}
		}
		if (!request)
			alert("Error initializing XMLHttpRequest!");
			
		return request;
	}
	
	function getCurrentFolder()
	{
		var p=""+document.location.pathname
		
		return p.substring(0,p.lastIndexOf('\\')+1)
	}
	function downloadRss()
	{
		var request=createRequest();
		request.open("GET", EslRssUrl, false);
		request.send("");
		var xmlDoc=request.responseXML;
		var items=xmlDoc.getElementsByTagName('item')
		
		for(var i=0;i<items.length;++i){
			// handle 1 rss item
			
			var title=items[i].getElementsByTagName('title')[0].text;
			var description=items[i].getElementsByTagName('description')[0].text
			var mp3Url=items[i].getElementsByTagName('enclosure')[0].getAttribute('url')
			
			
			var folderName=getCurrentFolder()+'output\\'+title
			{
				folderName=folderName.replace(/\?/g,'')
			}
			var textPath=folderName+'\\script.txt'
			var mp3Path=folderName+'\\audio.mp3'
			
			//try create folder
			{
				var fso = new ActiveXObject("Scripting.FileSystemObject");
				if(fso.FolderExists(folderName))
					continue;
				//alert(folderName)
				fso.CreateFolder(folderName)
			}
			
			//write text file
			{
				var file=fso.CreateTextFile(textPath,true);
				file.Write(description);
				file.Close();
			}
			
			//write audio file
			{
				download2(mp3Url,mp3Path);
			}
		}
	}

	function handleRss()
	{
		if (request.readyState == 4)
			alert("Server is done!");
	}
	downloadRss();
	
</script>